/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ClientsOfRestful;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.json.impl.provider.entity.JSONObjectProvider;
import java.net.URI;
import java.util.HashMap;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import org.json.simple.JSONObject;
import shoppingClient.ClientController;
import shoppingClient.Product;
import shoppingClient.ShoppingClientGUI;
/**
 *
 * @author truongtud
 */
public class ShoppingCartRestClientControler implements ClientController{
    static final String REST_URI ="http://localhost:9990/ebay";
    static final String ADD_USER="/shoppingcart/addUser";
    static final String ALL_PRODUCTS="/shoppingcart/getAllProduct";
    static final String ADD_PRODUCT="/shoppingcart/addProduct";
    static final String REMOVE_PRODUCT="/shoppingcart/removeProduct";
    static final String CHECK_OUT="/shoppingcart/checkOut";
    static final String SUB="/shoppingcart/sub";
    static ShoppingClientGUI clientFrame ;
    private HashMap<String, Product> allItem;
    Client client;
    WebResource service;
    public ShoppingCartRestClientControler(){
        ClientConfig config = new DefaultClientConfig();
        client = Client.create(config);
        service = client.resource(REST_URI);
        clientFrame = new ShoppingClientGUI(this);       
        
    }
    public static void main(String[]args){
                      
        ShoppingCartRestClientControler controller = new ShoppingCartRestClientControler();
         controller.clientFrame.setVisible(true);
        
         
         
    }
    private static String getResponse(WebResource service) {
        return service.accept(MediaType.TEXT_PLAIN).get(ClientResponse.class).toString();
    }
 
    private static JSONObject getOutputAsJSONObject(WebResource service) {
        return service.accept(MediaType.APPLICATION_JSON).post(JSONObject.class);
    }
     private static String getOutputAsText(WebResource service) {
         
      
       return service.accept(MediaType.TEXT_PLAIN).get(String.class);
     
    }
     private static String getOutputOfstandOfCheckOut(WebResource service){
         return service.accept(MediaType.TEXT_PLAIN).get(String.class);
     }

    @Override
    public void addUser(int id) {
        WebResource addUser=service.path(ADD_USER).path(new StringBuilder().append(id).toString());
       System.out.println(getOutputAsText(addUser));
    }

    @Override
    public boolean checkout(int id) {
        WebResource checkout =service.path(CHECK_OUT).path(new StringBuffer().append(id).toString());
        String checkoutstand =getOutputAsText(checkout);
        System.out.println("Luc checkout"+checkoutstand);
        if(checkoutstand.compareTo("true")==0)
            return true;
        else 
              return false;
    }

    @Override
    public HashMap<String, Product> load() {
        allItem = new HashMap<String, Product>();
        WebResource allProducts =service.path(ALL_PRODUCTS);    
        String products= getOutputAsText(allProducts);
        System.out.println(products);
        String[] productsinfo = products.split(":");
       // System.out.println(productsinfo[0]);
        for(String s : productsinfo){
            System.out.println(s);
            String[] temp = s.split("-");
            Product p = new Product(temp[0], Double.parseDouble(temp[1]), Integer.parseInt(temp[2]));
            allItem.put(temp[0], p);
        }
       return allItem;
    }

    @Override
    public void removeProduct(int id,String name) {
        WebResource remove =service.path(REMOVE_PRODUCT).path(new StringBuffer().append(id).toString()+"/"+name);    
//       String remove= getOutputAsText(addProduct);
    }

    @Override
    public void addProduct(int UserID ,String name) {
         WebResource addProduct =service.path(ADD_PRODUCT).path(UserID+"/"+name);    
        
             System.out.println( getOutputAsText(addProduct));
    }
    
    
}
